package com.lxw.report.meta.service.impl;

import com.lxw.report.meta.contract.BaseResponse;
import com.lxw.report.meta.contract.SysRole.SysRoleMenuCreateBatchRequest;
import com.lxw.report.meta.contract.SysUser.SysUserRoleCreateBatchRequest;
import com.lxw.report.meta.entity.Menu;
import com.lxw.report.meta.entity.Role;
import com.lxw.report.meta.entity.User;
import com.lxw.report.meta.enums.UserIdentityEnum;
import com.lxw.report.meta.mapper.SysMenuMapper;
import com.lxw.report.meta.mapper.SysUserMapper;
import com.lxw.report.meta.service.ComplexService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class ComplexServiceImpl implements ComplexService {
    @Autowired
    SysMenuMapper menuMapper;
    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public List<Menu> getMenuList(Long userId){
        User user = sysUserMapper.selectById(userId);
        if(user==null){
            return new ArrayList<>();
        }

        List<Menu> allMenuList=new ArrayList<>();



        if(UserIdentityEnum.isAdmin(user.getUserType())){
            allMenuList= menuMapper.selectList(null);
        }else{
            //todo 通过角色判断权限
            allMenuList= menuMapper.selectList(null);
        }

        //去除等级为空或者非顶级但是父级ID为空的脏数据
        Iterator<Menu> iterator = allMenuList.iterator();
        while (iterator.hasNext()){
            Menu menu=iterator.next();
            if(menu.getLevel()==null||menu.getLevel().intValue()!=1&&menu.getParentId()==null){
                iterator.remove();
            }
        }
        List<Menu> menuList = this.tranMenuList(allMenuList, 1, null);
        return menuList;
    }

    @Override
    public List<Role> getRoleList(Long userId) {
        return null;
    }

    @Override
    public BaseResponse addUserRoleBatch(SysUserRoleCreateBatchRequest request) {
        return null;
    }

    @Override
    public BaseResponse addRoleMenuBatch(SysRoleMenuCreateBatchRequest request) {
        return null;
    }

    /**
     * 转换菜单列表
     * @param allMenuList 当前所有的展平的菜单项
     * @param currentLevel 当前进行转换的菜单级别，从1开始
     * @param parentMenu 上级菜单
     * @return
     */
    private List<Menu> tranMenuList(List<Menu> allMenuList,int currentLevel,Menu parentMenu){
        List<Menu> menuList=new ArrayList<>();
            for(Menu menu:allMenuList){
                if(currentLevel==menu.getLevel().intValue()&&(parentMenu==null||menu.getParentId().longValue()==parentMenu.getId().longValue())){
                    menuList.add(menu);
                    List<Menu> childMenuList = tranMenuList(allMenuList, currentLevel + 1, menu);
                    if(CollectionUtils.isNotEmpty(childMenuList)){
                        menu.setRoutes(childMenuList);
                    }
                }
            }
        return menuList;
    }
}
